<?php
/**
 * 礼品兑换数据管理
 * PHP version 5
 * @package   Antonia
 * @author    Antonia <471699715@qq.com>
 * @copyright 2016 Antonia.
 */
namespace Admin\Controller;
use Think;
class ConvertController extends AdmincommonController {
    /**
     * 显示兑换记录列表
     * @param HttpRequest $request
     * @return html
     * @author Antonia
     */
    public function index() {
        $filters = array();
        !empty($_POST['giftname']) && $filters['giftName'] = array('like','%' . remove_xss($_POST['giftname']) . '%');
        if(!empty($_POST['convert_time'])) {
            $handle_time  = strtotime($_POST['convert_time']);
            $handle_times = strtotime($_POST['convert_times']);
            if($handle_time > $handle_times){
                 $filters['convert_time']  = array('between',array($handle_times,$handle_time));
            } else {
                $filters['convert_time']  = array('between',array($handle_time,$handle_times));
            }
        }
        $convert = D("Convert");
        $_GET['p'] = $_POST['p'];
        $count = $convert->where($filters)->count();
        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = 20;
        }
        $Page  =  new \Think\Page($count,$numPerPage);
        $list = $convert->limit($Page->firstRow.','.$Page->listRows)
                        ->relation(true)
                        ->where($filters)
                        ->select();
        $this->assign('list',$list);
        $this->assign('filters', $_POST);
        $this->assign ('totalCount', $count);
        $this->assign ('numPerPage', $Page->listRows);
        $this->assign ('currentPage', !empty($_GET['p'])?$_GET['p']:1);
        $this->display();
    }
    
    /**
     * 删除兑换记录
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delete() {
        $re = array();
        if (!empty($_GET['id'])) {
            $Convert = D("Convert");
            $result = $Convert->delete(remove_xss($_GET['id']));
            if (false !== $result) {
                $re['statusCode'] = "200";
                $re['message'] = "删除成功!";
            } else {
                $re['statusCode'] = "300";
                $re['message'] = "删除失败，请重试!";
            }
        } else {
                $re['statusCode'] = "300";
                $re['message'] = "该记录不存在！";
        }
        echo json_encode($re);
    }

    /**
     * 删除选定内容
     * @param HttpRequest $request
     * @return void
     * @author Antonia
    */
    public function delAll() {
        $Convert = D("Convert");
        $re = array();
        $map['id']  = array('in', explode(',', $_POST["ids"]));
        $result  = $Convert->where($map)->delete();
        if($result) {
            $re['statusCode'] = "200";
            $re['message'] = "删除成功!";
        } else {
            $re['statusCode'] = "300";
            $re['message'] = "删除失败，请重试!";
        }
        echo json_encode($re);
    }
}
?>